package com.zhu.service.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.model.pojo.Result;
import com.zhu.model.pojo.model.system.SysRole;

import com.zhu.model.pojo.model.system.vo.AssginRoleVo;
import com.zhu.service.system.service.IRoleService;
import com.zhu.model.pojo.model.system.vo.SysUserQueryVo;
import com.zhu.service.system.service.IUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-10-28
 */
@RestController
@RequestMapping("/admin/system/sysRole")
@CrossOrigin
@Api(tags = "角色模块")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IUserRoleService userRoleService;
    /**
     * =====================================用户===========================================================
     */
    /**
     *查询角色列表
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询角色列表")
    public List<SysRole> findAllRole(){
         return roleService.list();
    }
    /**
     *查询id查询角色
     */
    @GetMapping("/findById/{id}")
    @ApiOperation(value = "根据id查询角色")
    public Result findById(@PathVariable Long id){
        SysRole sysRole = roleService.getById(id);
        return Result.ok(sysRole);
    }
    /**
     * 根据id删除角色（逻辑删除）
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "根据id删除角色")
    public Result removeById(@PathVariable Long id){
        if(roleService.removeById(id)){
            return Result.ok().message("删除成功！");
        }
        return Result.fail().message("删除失败！");
    }
    /**
     * 条件分页查询
     */
    @GetMapping("2/{page}/{limit}")
    @ApiOperation(value = "条件分页查询（lamda）")
    public Result findQueryRole(@PathVariable Long page,
                                @PathVariable Long limit,
                                @RequestParam(required = false) String roleName){
        Page<SysRole>  pageParam=new Page<>(page,limit);
        IPage<SysRole> pageModel= roleService.selectPage(pageParam,roleName);
        return Result.ok(pageModel);
    }
    /**
     *
     */
    /**
     * 条件分页查询
     */
    @GetMapping("/{page}/{limit}")
    @ApiOperation(value = "条件分页查询2(sql语句)")
    public Result findQueryRole2(@PathVariable Long page,
                                @PathVariable Long limit,String  roleName){
        Page<SysRole>  pageParam=new Page<>(page,limit);
        System.out.println(roleName+"============");
        IPage<SysRole> pageModel= roleService.selectPage2(pageParam,roleName);
        return Result.ok(pageModel);
    }
    /**
     * 添加接口
     */
  @PostMapping("save")
  @ApiOperation(value = "添加角色")
    public Result saveRole(@RequestBody SysRole sysRole){
      if(roleService.save(sysRole)){
          return Result.ok().message("添加成功！");
      }
      return Result.fail().message("添加失败！");
  }

    @PostMapping("update")
    @ApiOperation(value = "修改角色")
    @Transactional(rollbackFor = Exception.class)
    public Result update(@RequestBody SysRole sysRole){
        if(roleService.updateById(sysRole)){
            return Result.ok().message("修改成功！");
        }
        return Result.fail().message("修改失败！");
    }

    @PostMapping("test")
    @ApiOperation(value = "测试")
    public Result test(@RequestBody SysUserQueryVo sysRole){
        System.out.println(sysRole);
            return Result.ok().message("添加成功！");
  }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("batchRemove")
    @ApiOperation(value = "批量删除")
    public Result batchRemove(@RequestBody List<Long> ids){
        if(roleService.removeByIds(ids)){
            return Result.ok().message("删除成功！");
        }
        return Result.fail().message("删除失败！");
    }
    /**
     * =====================================用户角色===========================================================
     */

    @ApiOperation(value = "根据用户id获取角色数据")
    @GetMapping("/doAssign/{userId}")
    public Result getRoleByUserId(@PathVariable Long userId){
       Map<String ,Object> roleMap= roleService.getRolesByUserId(userId);
       return Result.ok(roleMap);
    }

    @ApiOperation(value = "给用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
       Boolean result= roleService.assignRole(assginRoleVo);
       if(result){
           return Result.ok().message("分配成功！");
       }
       return Result.fail().message("分配失败！");

    }

}
